<?php
/**
 * Created by PhpStorm.
 * User: 晓典
 * Date: 2015/12/9 0009
 * Time: 10:40
 */

namespace App\Services\SizeService;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use Illuminate\Support\Facades\Log;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class BackstageSizeService
{

    public function getJsonSizeList(Request $request)
    {
        try{
            $prod_class_id=$request->input('prod_class_id');
            $size_list = DB::table('sizes')->where(array('state'=>1,'prod_class_id'=>$prod_class_id))
                                        ->value('size_list');
            if(!empty($size_list)){
                return explode('|',$size_list);
            }
            return array();
        }catch (\Exception $e){
            Log::info('********** 获取尺码列表 ************');
            Log::info($e);
            return array('data'=>array());
        }
    }

    /**
     * 获取产品分类下的尺码组
     * @param $prod_class_id
     * @return array
     */
    public function getSizeNameArrByProdClassId($prod_class_id){
        $size_list=  DB::table('sizes')->where(array('state'=>1,'prod_class_id'=>$prod_class_id))
            ->value('size_list');
        if(empty($size_list)){
            return array();
        }
        $size_arr=explode('|',$size_list);
        if(sizeof($size_arr)>2){
            $size_arr=array_filter($size_arr);
        }
        return $size_arr;
    }
    public function getSizeByProdClassId($prod_class_id){
        if($prod_class_id===0){
            $size=  DB::table('sizes')->where(array('state'=>1))
                ->first();
        }else{
            $size=  DB::table('sizes')->where(array('state'=>1,'prod_class_id'=>$prod_class_id))
                ->first();
        }
        if(empty($size)){
            return array();
        }
        $size->category_name=ProdTempCategoryTool::productTempCategoryNameByProdTempCategoryId($size->prod_class_id);
        $size_arr=explode('|',$size->size_list);
        if(sizeof($size_arr)>2){
            $size_arr=array_filter($size_arr);
        }
        return $size_arr;
    }
    /***
     * @param $size_name
     * @param $prod_class_id
     * @return bool
     */
    public function getSizeNameExistByProdClassId($size_name,$prod_class_id){
        if(empty($size_name)||empty($prod_class_id)){
            return false;
        }
        $size_name=trim($size_name);
        $size_arr=$this->getSizeNameArrByProdClassId($prod_class_id);
        if(empty($size_arr)){
            return false;
        }
        $has_size_name=in_array($size_name,$size_arr);
        return $has_size_name;
    }

    /***
     * 尺码名数组公式成字符串，中间'|'间隔
     * @param array $size_names
     * @return string
     */
    public function sizeNameArrFormatToStr(array  $size_names){
        if(sizeof($size_names)===0){
            return '';
        }
        $size_names_str='';
        foreach ($size_names as $size_name){
            $size_names_str.=$size_name.'|';
        }
        return $size_names_str;
    }

    /***
     * 添加尺码
     * @param array $size
     * @return bool
     */
    public function addSize(array  $size){
        $size_name=$size['size_name'];
        $prod_class_id=$size['prod_class_id'];
        $size_name_arr=$this->getSizeNameArrByProdClassId($prod_class_id);
        if(!empty($size_name_arr)){
            $size_name_arr[]=$size_name;
            $size_name_arr_str=$this->sizeNameArrFormatToStr($size_name_arr);
        }else{
            $size_name_arr_str=$size_name.'';
        }
        $size_name_arr_str=trim($size_name_arr_str);
       $size_list_exist=$this->prodSizeListExistByCategoryId($prod_class_id);
        $data['size_list']=$size_name_arr_str;
        $data['prod_class_id']=$prod_class_id;
        if($size_list_exist){
            $res=$this->prodSizeListUpdateByCategoryId($data);
        }else{
            $res=$this->prodSizeListSaveByCategoryId($data);
        }
        return $res;
    }
    public function prodSizeListUpdateByCategoryId(array  $data){
    if(!isset($data['prod_class_id'])||!is_int($data['prod_class_id'])){
        return false;
    }
     $prod_class_id=$data['prod_class_id'];
    $data['updated_at']=time();
    $data['state']=1;
     $num =  DB::table("sizes")->where(array('prod_class_id'=>$prod_class_id))->update($data );
    return ($num==1)?true:false;
    }
    public function prodSizeListSaveByCategoryId(array  $data){
        if(empty($data)){
            return false;
        }
        $data['updated_at']=time();
        $data['state']=1;
        $data['created_at']=time();
        $num =  DB::table("sizes")->insert($data );
        return ($num==1)?true:false;
    }
    public function prodSizeListExistByCategoryId($category_id,$state=0){
        if(!is_int($category_id)){
            return false;
        }
        if($state===0){
            $num =  DB::table("sizes")->where(array('prod_class_id'=>$category_id))->count();
        }else{
            $num =  DB::table("sizes")->where(array('prod_class_id'=>$category_id,'state'=>$state))->count();
        }
        return ($num==1)?true:false;
    }
    public function validator(array $size)
    {
        $v = [
            'size_name' => 'required|max:12',
            'prod_class_id'=>'required|integer|min:1',
        ];
        return  Validator::make(
            $size,$v

        );
    }

    /***
     * 获取模板下的尺码数组
     * @param $product_temp_id
     * @return array
     */
    public function getProductSizesByProductTempId($product_temp_id){
        $product_temp_id=trim($product_temp_id);
        if(empty($product_temp_id)){
            return array();
        }
        $product_temp_sizes= DB::table('product_temp_sizes')
            ->where(array('product_temp_id'=>$product_temp_id,'state'=>1))
            ->get()
            ->toArray();
        return $product_temp_sizes;
    }
}